<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>10.立方体纹理</title>
</head>
<body>
	<canvas id="canvas"></canvas>
	<script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/m4.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/primitives.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/texture-utils.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/chroma.min.js"></script>
	<script type="x-shader/x-vertex" id="vertex">
		attribute vec4 a_position;
		attribute vec3 a_normal;

		uniform mat4 u_projection;
		uniform mat4 u_view;
		uniform mat4 u_world;
		attribute vec2 a_texcoord;
		
		varying vec2 v_texCoord;

		varying vec3 v_normal;
		
		varying vec3 v_worldPosition;
		varying vec3 v_worldNormal;
		
		void main(){
			gl_Position =  u_projection * u_view * u_world * a_position;
			v_texCoord=a_texcoord;
			v_normal = normalize(a_position.xyz);
			
			v_worldPosition = (u_world * a_position).xyz;

			// orient the normals and pass to the fragment shader
			v_worldNormal = mat3(u_world) * a_normal;
		}
	</script>
	<script type="x-shader/x-fragment" id="fragment">
		precision mediump float;
		
		uniform samplerCube u_texture;

		varying vec3 v_normal;
		varying vec3 v_worldPosition;
		varying vec3 v_worldNormal;
		
		uniform vec3 u_worldCameraPosition;
		void main(){
			
			vec3 worldNormal = normalize(v_worldNormal);//法线
			vec3 eyeToSurfaceDir = normalize(v_worldPosition-u_worldCameraPosition);//物体坐标-相机
			vec3 direction = reflect(eyeToSurfaceDir,worldNormal);//反射

			gl_FragColor = textureCube(u_texture,direction);
		}
	</script>
	<script>
		const canvas = document.getElementById("canvas");
		canvas.width=window.innerWidth
		canvas.height=window.innerHeight
		const gl = canvas.getContext("webgl")
		const aspect = gl.canvas.clientWidth/ gl.canvas.clientHeight;
		const programInfo=webglUtils.createProgramInfo(gl,["vertex","fragment"])
		const planeBufferInfo = primitives.createCubeBufferInfo(
			gl,
			20
		);
		gl.useProgram(programInfo.program)
		const cameraMatrix = m4.lookAt([50,50,50],[0,0,0],[0,1,0])
		const viewMatrix=m4.inverse(cameraMatrix)
		function degToRad(d) {
			return d * Math.PI / 180;
		}
		
		var texture = gl.createTexture();
		gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
		const images = [
			{ target: gl.TEXTURE_CUBE_MAP_POSITIVE_X, image:new Image ,src:"/pos-x.jpg"},
			{ target: gl.TEXTURE_CUBE_MAP_NEGATIVE_X, image:new Image ,src:"/neg-x.jpg"},
			{ target: gl.TEXTURE_CUBE_MAP_POSITIVE_Y, image:new Image ,src:"/pos-y.jpg"},
			{ target: gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, image:new Image ,src:"/neg-y.jpg"},
			{ target: gl.TEXTURE_CUBE_MAP_POSITIVE_Z, image:new Image ,src:"/pos-z.jpg"},
			{ target: gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, image:new Image ,src:"/neg-z.jpg"},
		];
		const arrs=[]
		images.forEach((i)=>{
			arrs.push(new Promise(r=>{
				i.image.onload=(e=>{
					console.log("1:",1);
					r(()=>{
						gl.texImage2D(i.target, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, i.image);
					})
				})	
			}))
			i.image.src=i.src
		})
    var cameraPosition = [0, 0, 2];
	var world=m4.translation(0, 0, 0)
	let t=0
  var worldCameraPositionLocation = gl.getUniformLocation(programInfo.program, "u_worldCameraPosition");
		Promise.all(arrs).then(r=>{
			r.forEach(i=>{
				i()
			})
			gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
			gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
			var textureLocation = gl.getUniformLocation(programInfo.program, "u_texture");
setInterval(()=>{
				
	world = m4.yRotate(world, 0.001)
			gl.uniform1i(textureLocation, 0);
			const projectionMatrix=m4.perspective(degToRad(60), aspect, 1, 2000);
			webglUtils.setBuffersAndAttributes(gl, programInfo, planeBufferInfo);
			webglUtils.setUniforms(programInfo, {
				u_view: viewMatrix,					//视图
				u_projection: projectionMatrix,		//投影
				u_world:world,	//世界
			});
			
			gl.uniform3fv(worldCameraPositionLocation, cameraPosition);
			gl.enable(gl.CULL_FACE);
			gl.enable(gl.DEPTH_TEST);
			webglUtils.drawBufferInfo(gl, planeBufferInfo);
},0)
		})
	</script>
</body>
</html>